<?php
class Login extends CI_Controller{
    public function __construct() {
        parent::__construct();
        if(is_login()) {
            error();
        }
        $this->load->model('personnel_model');
        $this->load->library('encrypt');
        $this->load->helper('cookie');
    }
    function index(){
         if(isset($_COOKIE['pmlg'])){
                $user = @split('---',$this->encrypt->decode($_COOKIE['pmlg']));
                $arr = $this->personnel_model->login($user[0],$user[1]);
                if($arr==null){
                    redirect(base_url().'login');
                }else {
                    $this->session->set_userdata($arr);
                    routing_admin();
                }
        }
        $this->loadlogin();
    }
        function loginprocess(){     
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));
        $re = $this->security->xss_clean($this->input->post('remember'));
        if($this->do_login($username, $password)){
            if($re=='on'){
                $mycookies = $this->encrypt->encode(implode('---',
                        array($username,$password)
                ));
                $myDomain = ereg_replace('^[^\.]*\.([^\.]*)\.(.*)$', '\1.\2',  base_url());  
                $setDomain = ($_SERVER['HTTP_HOST']) != "localhost" ? ".$myDomain" : false;  
                setcookie ("pmlg", $mycookies, time()+3600*24*(2), '/', "$setDomain", 0 );
                
            }
            routing_admin();
        }else{
            $this->loadlogin('Sai tên đăng nhập hoặc mật khẩu');
        }
    }
    function loadlogin($mes=null){
        $this->load->view('personnel/login_view',array('mes'=>$mes));
    }
    function do_login($username,$password){
        $arr = $this->personnel_model->login($username,$password);
        if($arr==null) {
            return false;
        }else {
             $this->session->set_userdata($arr);
             return true;
        }
    }
}
?>
